package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import www.lagou.entity.Account;
import www.lagou.entity.Transaction;
import www.lagou.utils.DateUtils;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;

/**
 * 账户类的数据操作
 */
public class AccountDao {

    // 使用Druid连接池自动创建QueryRunner对象
    private QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    public void transAccounts(String transOut,String transIn,double transMoney){
        // 获取转出、转入账户余额
        Account transOutAccount = getBalanceByCard(transOut);
        Account transInAccount = getBalanceByCard(transIn);
        // 若账户存在，考虑是否可以转账
        if (transOutAccount != null && transInAccount != null){
            // 判断转出账户余额是否大于转账金额
            if (transOutAccount.getBalance() > transMoney){
                // 执行转账
                transOutAccount.setBalance(transOutAccount.getBalance() - transMoney);
                transInAccount.setBalance(transInAccount.getBalance() + transMoney);
                // 写入数据库
                int transOutCount = updateAccount(transOutAccount);
                int transInCount = updateAccount(transInAccount);
                if (transOutCount == 1 && transInCount == 1){
                    Transaction transaction1 = new Transaction(transOutAccount.getCard(),"转出",transMoney,DateUtils.getCurrentTime());
                    Transaction transaction2 = new Transaction(transInAccount.getCard(),"转入",transMoney,DateUtils.getCurrentTime());
                    insertTransRecord(transaction1);
                    insertTransRecord(transaction2);
                    System.out.println("转账成功！");
                }else
                    System.out.println("转账失败！");


            }else {
                System.out.println("余额不足！");
                return;
            }
        }

    }

    /**
     * 通过账户号查找用户
     * @param card 账户号
     * @return 用户
     */
    public Account getBalanceByCard(String card){
        Account account = new Account();
        try {
            String sql = "select * from account where card = ?";
            account = queryRunner.query(sql, new BeanHandler<Account>(Account.class), card);
            if (account != null){
                return account;
            }else{
                System.out.println(card + " 未开户！");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return account;
    }

    /**
     * 更新账户余额
     * @param account 账户
     * @return 更新条数
     */
    public int updateAccount(Account account){
        int update = 0;
        try {
            String sql = "update account set balance = ? where card = ?";
            Object[] objects = {account.getBalance(),account.getCard()};
            update = queryRunner.update(sql, objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;
    }

    public int insertTransRecord(Transaction transaction){
        int update = 0;
        try{
            String sql = "insert into TRANSACTION values (null,?,?,?,?)";
            Object[] objects = {transaction.getCardid(),transaction.getTratype(),transaction.getTramoney(),transaction.getTradate()};
            update = queryRunner.update(sql, objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;
    }
}
